package main

import (
	"JVM/Jvmgo/GoToJVM/classfile"
	"JVM/Jvmgo/GoToJVM/classpath"
	"fmt"
	"strings"
)

func main() {
	cmd := parseCmd()
	if cmd.versionFlag {
		fmt.Println("version 0.0.1")
	} else if (cmd.helpFlag || cmd.class == "") && cmd.JavapOption == "" {
		printUsage()
	} else if cmd.JavapOption != "" {
		printMyFile(cmd)
	} else {
		newJVM(cmd).start()
	}
}

// javap
func printMyFile(cmd *Cmd) {
	classPath := cmd.JavapOption
	classData := classpath.GetJpPath(classPath)
	//for _, b := range classData {
	//	fmt.Printf("%02X ", b)
	//}
	cf, err := classfile.Parse(classData)
	if err != nil {
		panic(err)
	}
	className := strings.Replace(cmd.class, ".", "/", -1)
	fmt.Println(className)
	printClassInfo(cf)
	//fmt.Println("constantPool: ", cf.ConstantPool())
}
func printClassInfo(cf *classfile.ClassFile) {
	fmt.Printf("version: %v.%v\n", cf.MajorVersion(), cf.MinorVersion())
	fmt.Printf("constants count: %v\n", cf.ConstantPoolCount())
	fmt.Printf("access flags: 0x%x\n", cf.AccessFlags())
	fmt.Printf("this class: %v\n", cf.ClassName())
	fmt.Printf("super class: %v\n", cf.SuperClassName())
	fmt.Printf("interfaces count: %v\n", cf.InterfacesCount())
	fmt.Printf("interfaces: %v\n", cf.InterfaceNames())
	fmt.Printf("fields count: %v\n", cf.FieldsCount())
	for _, f := range cf.Fields() {
		fmt.Printf("  %s\n", f.Name())
	}
	fmt.Printf("methods count: %v\n", cf.MethodsCount())
	for _, m := range cf.Methods() {
		fmt.Printf("  %s\n", m.Name())
	}
}
